package com.renzhebiao.lkang.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.renzhebiao.lkang.Interceptor.EventLogInterceptor;
import com.renzhebiao.lkang.Interceptor.TokenAuthenticationInterceptor;

/**
 * 加载拦截器
 * 
 * @author Fintan 2018年2月8日
 */
@Configuration
public class SessionConfiguration extends WebMvcConfigurerAdapter {

    // private TokenInterceptor tokenInterceptor;
    // private JWTUtil jwtUtil;
    //
    // @Autowired
    // public SessionConfiguration(TokenInterceptor tokenInterceptor, JWTUtil jwtUtil) {
    // this.jwtUtil = jwtUtil;
    // this.tokenInterceptor = tokenInterceptor;
    // }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(new EventLogInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new TokenAuthenticationInterceptor()).addPathPatterns("/**").excludePathPatterns("/swagger*/**",
                                                                                                                 "/v2/**",
                                                                                                                 "/webjars/**",
                                                                                                                 "/hello");
        // registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger*/**",
        // "/v2/**",
        // "/webjars/**", "/hello");
        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars*").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
